package com.ruoyi.resume.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.resume.domain.model.Category;
import com.ruoyi.resume.domain.model.ResumeTag;
import com.ruoyi.resume.domain.model.Tag;
import com.ruoyi.resume.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "标签管理")
@RestController
@RequestMapping("/labeling")
public class LabellingController {

    @Autowired
    TagService tagService;

    @ApiOperation(value = "查询简历ID对应的标签")
    @GetMapping("/search-resume-tags/{resumeId}")
    @Anonymous
    public ResponseEntity<Map<String, Integer>> getTagsByResumeId(
            @ApiParam(value = "简历ID", required = true) @PathVariable Integer resumeId) {
        Map<String, Integer> tags = tagService.getTagsByResumeId(resumeId);
        return ResponseEntity.ok(tags);
    }

    @ApiOperation(value = "查询所有标签内容")
    @GetMapping("/all")
    @Anonymous
    public ResponseEntity<List<Category>> getAllTags() {
        List<Category> categories = tagService.getAllTags();
        return ResponseEntity.ok(categories);
    }

    @ApiOperation(value = "新建标签")
    @PostMapping("/create-tag")
    @Anonymous
    public ResponseEntity<String> createTag(
            @ApiParam(value = "标签对象", required = true) @RequestBody Tag tag) {
        Integer newId = tagService.createTag(tag);
        if (null!=newId) {
            return ResponseEntity.ok("标签创建成功,id:" + newId);
        } else {
            return ResponseEntity.status(500).body("标签创建失败");
        }
    }

    @ApiOperation(value = "删除标签")
    @DeleteMapping("/delete-tag/{id}")
    @Anonymous
    public ResponseEntity<String> deleteTagById(
            @ApiParam(value = "标签ID", required = true) @PathVariable Integer id) {
        boolean isDeleted = tagService.deleteTagById(id);
        if (isDeleted) {
            return ResponseEntity.ok("标签删除成功");
        } else {
            return ResponseEntity.status(404).body("标签不存在或删除失败");
        }
    }

    @ApiOperation(value = "修改标签")
    @PutMapping("/update-tag")
    @Anonymous
    public ResponseEntity<String> updateTag(
            @ApiParam(value = "标签对象", required = true) @RequestBody Tag tag) {
        boolean isUpdated = tagService.updateTag(tag);
        if (isUpdated) {
            return ResponseEntity.ok("标签修改成功");
        } else {
            return ResponseEntity.status(404).body("标签不存在或修改失败");
        }
    }


}
